﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using CustomerEmailer.Common;

namespace CustomerEmailer
{
    public class ErrorLog
    {
        private string LogeFile
        {
            get
            {
                return Utils.GetSetting("ErrorFile");
            }
        }

        public void WriteException(Exception ex)
        {
           var logBuilder = new StringBuilder();
            logBuilder.AppendFormat("*************************************");
            logBuilder.AppendLine();
            logBuilder.AppendLine("Message:");
            logBuilder.AppendLine(ex.Message);
            logBuilder.AppendLine("Stacktrace:");
            logBuilder.AppendLine(ex.StackTrace);
            logBuilder.Append("-------------------------------------------");
            WriteToLog(logBuilder.ToString());
        }

        void WriteToLog(string content)
        {
            StreamWriter log = null;
            if (!File.Exists(LogeFile))
            {
                var stream  = File.Create(LogeFile);
                log = new StreamWriter(stream);
            }
            else
            {
                 log = File.AppendText(LogeFile);
            }
            log.WriteLine(content);
            log.Close();
        }
    }

}
